package com.biao.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biao.common.utils.PageUtils;
import com.biao.common.utils.Query;
import com.biao.mall.product.dao.BrandDao;
import com.biao.mall.product.dao.CategoryBrandRelationDao;
import com.biao.mall.product.entity.BrandEntity;
import com.biao.mall.product.service.BrandService;
import com.biao.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<BrandEntity> wrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        wrapper.and(StringUtils.isNotBlank(key), lambdaQueryWrapper -> lambdaQueryWrapper
                .eq(BrandEntity::getBrandId, key)
                .or()
                .like(BrandEntity::getName, key)
        );

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        updateById(brand);//先更新自己表

        //更新别处关联
        if (!StringUtils.isBlank(brand.getName())) {
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //TODO
        }
    }

}